package com.nuc.iblog.service.imp;


import com.nuc.iblog.entity.Article;
import com.nuc.iblog.entity.ArticlePage;
import com.nuc.iblog.entity.Category;
import com.nuc.iblog.entity.User;
import com.nuc.iblog.jpa.ArticleJpa;
import com.nuc.iblog.jpa.CategoryJpa;
import com.nuc.iblog.jpa.CommentsJpa;
import com.nuc.iblog.jpa.UserJpa;
import com.nuc.iblog.service.ClArticleService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Tyranitarx on 2018/1/11.
 *
 * @Description :
 */
@Service
public class ClClArticleServiceImp implements ClArticleService {
    Logger log = org.slf4j.LoggerFactory.getLogger(ClClArticleServiceImp.class);
    RedisTemplate redisTemplate;
    @Autowired
    private ArticleJpa articleJpa;
    @Autowired
    private UserJpa userJpa;
    private Article article;
    private List<Article> articles;


    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);

        this.redisTemplate = redisTemplate;
    }


    /**
     * 通过aid获取相应的文章
     *
     * @param aid
     * @return
     */
    @Override
    public Article getArticle(int aid) {
        article = articleJpa.findByAid(aid);
        int loves=Integer.parseInt((String) redisTemplate.opsForValue().get(String.valueOf(aid)));
        article.setLove(loves);
        return article;
    }

    private int num;
    private List<Article> returnArticle;

    /**
     * 获取首页文章
     *
     * @return
     */
    @Override
    public List<Article> getArticles() {
        returnArticle=new ArrayList<Article>();
        for(int i=19;i<=21;i++) {
            article = articleJpa.findByAid(i);
            article.setContent(article.getContent().replaceAll("\n",""));
            returnArticle.add(article);
        }
        return returnArticle;
    }


    private User user;

    /**
     * 获取用户个人文章
     *
     * @param uid
     * @return
     */
    @Override
    public List<Article> getArticlesByUser(int uid) {
        user = userJpa.findByUid(uid);
        return user.getArticles();
    }

    private List<Article> articleList;
    private ArticlePage articlePage;

    @Override
    public ArticlePage getAcademyArticle(int catid, int pagenum) {
        int page = 10;
        int totalpage;
            articlePage = new ArticlePage();
            articleList = articleJpa.findArticleByAcademy(catid);
            int lastpage = articleList.size() % page;
        if (articleList.size()!=0) {
            if (lastpage == 0)
                totalpage = articleList.size() / page;
            else
                totalpage = articleList.size() / page + 1;
            if (pagenum + 1 == totalpage) {
                articlePage.setContent(articleList.subList(pagenum * 10, articleList.size()));
                articlePage.setTotalpage(totalpage);
                return articlePage;
            } else {
                articlePage.setContent(articleList.subList(pagenum * 10, (pagenum + 1) * 10));
                articlePage.setTotalpage(totalpage);
                return articlePage;
            }
        }else {
            articlePage.setContent(articleList);
            articlePage.setTotalpage(0);
            return articlePage;
            }
        }


    /**
     * 用户添加文章
     *
     * @param uid
     * @param content
     * @return
     */
    @Override
    public int insertArticle(int uid,String title, String summary, String content) {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        article = new Article();
        article.setUser(userJpa.findByUid(uid));
        article.setDate(sdf.format(new Date()));
        article.setContent(content);
        article.setSummary(summary);
        article.setTitle(title);
        article=articleJpa.save(article);
        redisTemplate.opsForValue().set(String.valueOf(article.getAid()),String.valueOf(article.getLove()));
        return 1;
    }

    @Override
    public int updateArticle(int uid, int aid,String title, String summary, String content) {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        article = articleJpa.findByAid(aid);
        article.setUser(userJpa.findByUid(uid));
        article.setDate(sdf.format(new Date()));
        article.setContent(content);
        article.setSummary(summary);
        article.setTitle(title);
        articleJpa.save(article);
        return 1;
    }

    @Autowired
    private CommentsJpa commentsJpa;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteArticle(int aid) {
        commentsJpa.deleteCommentsByArticle(articleJpa.findByAid(aid));
        return articleJpa.deleteArticleByAid(aid);
    }

    @Override
    public Page<Article> getPageArticle(int uid, int pagenum,int size) {
        Pageable pageable = new PageRequest(pagenum, size);
        return articleJpa.findByUserOrderByAidDesc(userJpa.findByUid(uid), pageable);
    }

    @Override
    public List<Article> searchArticle(String input) {
        return articleJpa.searchArticle(input);
    }
}
